import { Message } from "element-ui";
import Vue from 'vue'
import Vuex from 'vuex'
import moment from "moment";
import { number } from "echarts";
import vm from "../main";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    roelMenu: [],//菜单权限
    roleBtn: [],//按钮权限
    userToken: "",
    user: {},//登录者信息
    depart: {},//部门
    callToken: {},//呼叫中心token
    showToolBarModel: false,//来电弹框
    btnDisabled: false,//按钮二次防点击
    topMenus: [],
  },
  mutations: {
    //登录者信息
    ROLEMENU(state, data) {
      state.roleMenu = data[0];
      state.roleBtn = data[1];
      state.userToken = data[2];
      state.user = data[3];
      state.depart = data[4];
    },
    //呼叫中心token
    CALLTOKEN(state, data) {
      state.callToken = data;
    },
    //退出登录
    LOGINOUT(state, data) {
      state.token = "";
    },
    // 修改个人信息
    EIDITUSER(state, data) {
      state.user.userName = data.userName;
      state.user.userEmail = data.userEmail;
    },
    //来电弹框
    INCOMING(state, data) {
      state.showToolBarModel = data
    },
    // 改变按钮状态
    CHANGEBTNSTATUS(state, data) {
      state.btnDisabled = data;
    },
    // 移某个多标签导航
    DELSELMENUS(state, data) {
      state.topMenus.splice(data, 1)
    },
    // 清空多标签导航
    CLEARTOPMENUS(state, data) {
      state.topMenus = []
    },
    // 只保留当前标签
    ONLYSAVEONE(state, data) {
      state.topMenus = [];
      state.topMenus.push(data);
    },
  },
  actions: {
  },
  getters: {
    //控制按钮显示与隐藏
    returnBtnRole(state) {
      return function (btnsName) {
        // 过滤出按钮名
        let btnNames = [];
        state.roleBtn.forEach(item => {
          btnNames.push(item.authorityName);
        });
        if (btnNames.includes(btnsName)) {
          return true;
        } else {
          return false;
        };
      }
    },
    // 时间改造
    timer(state) {
      return function (time) {
        if (time != null) {
          return moment(time).format(
            "YYYY-MM-DD"
          );
        } else {
          return time
        }

      }
    },
    timer3(state) {
      return function (time) {
        if (time != null) {
          return moment(time).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        } else {
          return time
        }
      }
    },
    timer2(state) {
      return function (time) {
        if (time != null) {
          time = Number(time)
          let h = 0,
            m = 0,
            s = 0;
          h = Math.floor(time / 3600);
          m = parseInt((time % 3600) / 60);
          s = time % 60;
          if (h < 9) {
            h = "0" + h;
          }
          if (m < 9) {
            m = "0" + m;
          }
          if (s < 9) {
            s = "0" + s;
          }
          return `${h}:${m}:${s}`;
        } else {
          return time
        }
      }
    },
    number(state) {
      return function (num) {
        if (num < 0) {
          return 0
        } else {
          return num
        }
      }
    },
    // 保留两位小数
    number1(state) {
      return function (egnum) {
        if (egnum) {
          let num = parseFloat(egnum).toFixed(2);;
          return num
        } else {
          return "0.00"
        }
      }
    },
    // 千分位
    number2(state) {
      return function (egnum) {
        if (egnum) {
          let num = parseFloat(egnum);
          return (num + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
        } else {
          return "0.00"
        }
      }
    },
    // 千分位,保留两位小数
    number3(state) {
      return function (egnum) {
        if (egnum) {
          let num = Number(egnum).toFixed(2);
          return (num + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
        } else {
          return "0.00"
        }
      }
    },
    // 千分位,保留4位小数
    number4(state) {
      return function (egnum) {
        if (egnum) {
          let num = Number(egnum).toFixed(4);
          return (num + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
        } else {
          return "0.00"
        }
      }
    },
    //销毁当前页面缓存
    distroyNowKeepAlive(state) {
      return function (obj) {
          /**=======================清除keepAlive缓存核心代码========================= */
        let arr = vm.$children[0]._vnode.children[0].componentInstance.$children;
        let l2 = state.topMenus.length;
        arr.forEach((item2, i) => {
          if (
            item2.$vnode.data.key == state.topMenus[l2 - 1].fullPath
          ) {
            // 删除cache中的记录
            for (let key in item2.$vnode.parent.componentInstance.cache) {
              if (obj.fullPath == key) {
                delete item2.$vnode.parent.componentInstance.cache[key];
              }
            }
            // 因为在cache和keys中都有记录，所以保险起见将keys中的记录也删除(没删这个也一样的，试了)
            item2.$vnode.parent.componentInstance.keys.forEach((item3, i) => {
              if (obj.fullPath == item3) {
                item2.$vnode.parent.componentInstance.keys.splice(i, 1);
              }
            });
          }
        });
        // /**=======================END========================= */
        state.topMenus.forEach((item, i) => {
          if (item.fullPath == obj.fullPath) {
            state.topMenus.splice(i, 1)
          }
        })
        let l = state.topMenus.length;
        vm.$router.push({
          path:
            l > 0
              ? state.topMenus[l - 1].fullPath
              : `/${state.roleMenu[0].children[0].path}`,
        });
      }
    }
  },
  modules: {
  }
})
